//
//  YZForgetPWDVC.m
//  VET
//
//  Created by 刘文靖 on 2018/7/6.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZForgetPWDVC.h"
#import "YZGradientBtn.h"
#import "YZResetPasswordVC.h"
#import "NSString+Predicate.h"
#import "YZPersonalVM.h"
@interface YZForgetPWDVC ()
@property (weak, nonatomic) IBOutlet UITextField *mobileTF;
@property (weak, nonatomic) IBOutlet UITextField *verCodeTF;
@property (weak, nonatomic) IBOutlet YZGradientBtn *verCodeButton;
@property (weak, nonatomic) IBOutlet YZGradientBtn *nextButton;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@end

@implementation YZForgetPWDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    self.viewModel = [[YZPersonalVM alloc] init];
    RACSignal *mobileSignal = [self.mobileTF.rac_textSignal map:^id(NSString *str) {
        return @([str isValidMobileNumber]);
    }];
    RACSignal *verCodeSignal = [self.verCodeTF.rac_textSignal map:^id(NSString *str) {
        return @(str.length);
    }];
    RAC(self.verCodeButton, enabled) = mobileSignal;
    RAC(self.nextButton, enabled) = [RACSignal combineLatest:@[mobileSignal,
                               verCodeSignal]
                      reduce:^(NSNumber *mobileValid, NSNumber *verCodeValid)
     {
         return @(mobileValid.boolValue && [verCodeValid boolValue]);
     }];
    
}
- (IBAction)verCodeButtonClick {
    @weakify(self)
    [[self.viewModel requestMessageForPWD:self.mobileTF.text] subscribeNext:^(id  _Nullable x) {
    } completed:^{
        @strongify(self)
        [self.verCodeButton startCount:60];
    }];
}
- (IBAction)nextButtonClicked:(id)sender {
    YZResetPasswordVC *resetPWDVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZResetPasswordVC"];
    resetPWDVC.title = @"重置密码";
    resetPWDVC.mobile = self.mobileTF.text;
    resetPWDVC.verCode = self.verCodeTF.text;
    [self.navigationController pushViewController:resetPWDVC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

@end
